package com.datasure.application.service.impl;

import com.datasure.application.res.assembler.DeviceEntityAssembler;
import com.datasure.application.res.dto.SourceDeviceShowDTO;
import com.datasure.application.res.dto.SourceDeviceShowStatisticsDTO;
import com.datasure.application.service.SourceShowSerice;
import com.datasure.domain.entity.*;
import com.datasure.domain.service.SourceDeviceDomainService;
import com.datasure.repository.*;
import com.datasure.types.IntegerId;

import javax.xml.bind.ValidationException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangxiaolong
 * @ClassName SourceShowServiceImpl.java
 * @Description
 * @createTime 2022年02月08日 16:16:00
 */
public class SourceShowServiceImpl implements SourceShowSerice {

    private DeviceRepository deviceRepository;
    private DeviceExRepository deviceExRepository;
    private DiskCloneRepository diskCloneRepository;
    private DiskCloneExRepository diskCloneExRepository;
    private VersionRepository versionRepository;

    private DeviceEntityAssembler deviceEntityAssembler = DeviceEntityAssembler.INSTANCE;

    private SourceDeviceDomainService sourceDeviceDomainService;

    /**
     * 源设备列表信息统计
     *
     * @return todo 类型换位实际dto
     */
    public SourceDeviceShowStatisticsDTO showListStatistics(){
        SourceDeviceShowStatisticsDTO sourceDeviceShowStatisticsDTO = new SourceDeviceShowStatisticsDTO();

        List<Device> allSourceList = deviceRepository.getAllSourceList();
        for(Device device : allSourceList){
            //通过状态统计
            int sourceState = device.getSourceState(sourceDeviceDomainService);

        }

        return sourceDeviceShowStatisticsDTO;
    }

    /**
     * 源设备列表查询
     * @return
     */
    public List<SourceDeviceShowDTO> showList(String inputState) throws ValidationException {

        //查询所有的源端设备
        List<Device> allSourceList = deviceRepository.getAllSourceList();

        //根据传入的状态值过滤设备
        List<Device> sourceList = allSourceList.stream().filter(device -> device.sourceFilter(inputState)).collect(Collectors.toList());

        //List<IntegerId> deviceIds = sourceList.stream().map(device -> device.getDeviceId()).collect(Collectors.toList());


        /**
         * 将多表关联查询拆分为多个查询，在应用层中组装；不能这样写，要在领域服务中使用依赖
         * ***/
        /*
        //获取deviceEx相关信息
        List<DeviceEx> deviceExList = deviceExRepository.findByIds(deviceIds);
        Map<StringId, DeviceEx> deviceExMap = deviceExList.stream().collect(Collectors.toMap(DeviceEx::getDeviceUniqueId, deviceEx -> deviceEx));

        List<DiskClone> diskCloneList = diskCloneRepository.findListByDeviceId(deviceIds);
        Map<IntegerId, DiskClone> diskCloneMap = diskCloneList.stream().collect(Collectors.toMap(DiskClone::getDeviceId, diskClone -> diskClone));

        List<IntegerId> diskcloneIds = diskCloneList.stream().map(diskClone -> diskClone.getDiskcloneId()).collect(Collectors.toList());

        List<DiskCloneEx> diskCloneExList = diskCloneExRepository.findByDiskcloneIds(diskcloneIds);
        Map<IntegerId, DiskCloneEx> diskCloneExMap = diskCloneExList.stream().collect(Collectors.toMap(DiskCloneEx::getDiskcloneId, diskCloneEx -> diskCloneEx));*/


        //获取系统版本信息
        Vertion vertion = versionRepository.find(new IntegerId(10));


        //拼装返回信息
        List<SourceDeviceShowDTO> sourceDeviceShowDTOList = sourceList.stream().map(device -> {
            //使用mapstruct转换entity到dto
            SourceDeviceShowDTO sourceDeviceShowDTO = deviceEntityAssembler.toSourceDeviceShowDTO(device);
            //获取关联任务信息
            if (device.sourceHasTask(sourceDeviceDomainService)) {
                //DiskClone diskClone = diskCloneMap.get(device.getDeviceId());
                String sourceTaskName = device.getSourceTaskName(sourceDeviceDomainService);

                //设置关联任务名称
                //sourceDeviceShowDTO.setSourceTaskName(sourceTaskName);
            }


            //设置版本信息
            //sourceDeviceShowDTO.setVertion();

            return sourceDeviceShowDTO;
        }).collect(Collectors.toList());

        return sourceDeviceShowDTOList;
    }

}
